<template>
  <div class="addbox">
    <el-form
      ref="refform"
      inline
      :rules="rules"
      :model="form"
      label-width="80px"
    >
      <el-form-item prop="emName" label="员工姓名" size="small">
        <el-input
          v-model="form.emName"
          maxlength="10"
          show-word-limit
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <br />
      <el-form-item label="员工账户" size="small">
        <el-input
          v-model="form.loginID"
          clearable
          placeholder="请输入账户"
        ></el-input>
      </el-form-item>
      <br />
      <el-form-item label="登录密码" size="small">
        <el-input
          v-model="form.pwd"
          show-password
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <br />

      <el-form-item label="手机号码" size="small">
        <!-- 1.属性 prefix-icon='el-icon-message' -->
        <el-input v-model="form.phone" clearable placeholder="请输入密码">
          <!-- slot 插槽 -->
          <i slot="prefix" class="el-icon-phone"></i>
        </el-input>
      </el-form-item>
      <br />
      <el-form-item label="邮箱地址" prop='email' size="small">
        <!-- 1.属性 prefix-icon='el-icon-message' -->
        <el-input v-model="form.email" clearable placeholder="请输入密码">
          <!-- slot 插槽 -->
          <i slot="prefix" class="el-icon-message"></i>
        </el-input>
      </el-form-item>
      <br />
      <el-form-item label="性别" size="small">
        <el-radio-group v-model="form.emSex">
          <el-radio-button label="男"></el-radio-button>
          <el-radio-button label="女"></el-radio-button>
        </el-radio-group>
      </el-form-item>
      <br />
      <el-form-item label="兴趣爱好" size="small">
        <el-checkbox-group :max="2" :min="1" v-model="form.hobby">
          <el-checkbox
            border
            v-for="item in hobbys"
            :key="item"
            :label="item"
            :value="item"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <br />
      <el-form-item label="年龄" size="small">
        <!-- controls-position='right' -->
        <el-input-number
          :step="2"
          step-strictly
          v-model="form.age"
          :min="1"
          :max="99"
          label="label"
        ></el-input-number>
      </el-form-item>
      <br />

      <el-form-item label="角色">
        <el-select v-model="form.roleID" placeholder="请选中角色">
          <el-option
            v-for="item in roles"
            :key="item.roleID"
            :label="item.roleName"
            :value="item.roleID"
          ></el-option>
        </el-select>
      </el-form-item>

      <br />
      <el-form-item label="状态">
        <el-switch
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="正常"
          inactive-text="冻结"
          v-model="form.status"
        ></el-switch>
      </el-form-item>
      <br />
      <el-form-item label="备注">
        <el-input type="textarea" autosize v-model="form.mark"></el-input>
      </el-form-item>
      <br />
      <el-form-item label=" ">
        <el-button @click="addsave()" type="primary">立即添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    //编写的自定义验证规则的函数
    var emailvalidator=function(rule, value, callback){
        if(!value){
            return callback(new Error('邮箱不能为空'));
        }
        else if(!/^\w+@\w+.\w+$/.test(value)){
            return callback(new Error('请输入正确的邮箱地址'));
        }
        else{
            callback();
        }
    }
    return {
      hobbys: ["篮球", "足球", "排球", "网球"],
      roles: [
        { roleID: 1, roleName: "管理员" },
        { roleID: 2, roleName: "总经理" },
        { roleID: 3, roleName: "采购经理" },
        { roleID: 4, roleName: "采购员" },
      ],
      form: {
        emName: "",
        loginID: "",
        pwd: "",
        phone: "",
        email: "",
        emSex: "女",
        hobby: ["足球", "网球"],
        age: 18,
        roleID: 2,
        status: true,
        mark: "",
        headImg: "",
      },

      //表单验证规则
      rules: {
        emName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        email:[
            //validator指定 自定义验证规则
            { validator:emailvalidator,trigger: 'blur' }
        ]
      },
    };
  },
  methods: {
    addsave() {
      //先判断验证是否通过  refform 是form 上 ref 的值
      this.$refs.refform.validate((v) => {
        if (v) {
          let that = this;
          axios({
            method: "post",
            url: "https://localhost:44314/api/Employee",
            data: that.form,
          })
            .then(function (response) {
              if (response.data) {
                alert("添加成功");
              } else {
                alert("添加失败");
              }
            })
            .catch(function (error) {
              console.error(error);
            });
        }else{
            alert('请按要求输入数据');
        }
      });
    },
  },
};
</script>

<style>
.addbox {
  margin: 30px;
}
</style>